package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.OrdersBody;
import com.zzyl.nursing.dto.TimeBody;
import com.zzyl.nursing.mapper.OrdersReservationMapper;
import com.zzyl.nursing.service.OrdersReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdersReservationServiceImpl extends ServiceImpl<OrdersReservationMapper, Reservation> implements OrdersReservationService {
    @Autowired
    private OrdersReservationMapper orm;

    @Override
    public Long getCancelledCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<>();
        wrapper.between( Reservation::getUpdateTime, startTime, endTime)
                .eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, 2);
        return orm.selectCount(wrapper);
    }

    @Override
    public List<Reservation> countByTime(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(Reservation::getTime, startTime, endTime)
                .ne(Reservation::getStatus, 2)
                .groupBy(Reservation::getTime);
        TimeBody tb = new TimeBody();
        tb.setTime(DateUtil.date(time));
        tb.setStartTime(DateUtil.date(startTime));
        tb.setEndTime(DateUtil.date(endTime));
        List<Reservation> list = orm.selectLists(tb);
        System.out.println("dadasdad:"+list);
        return list;
    }

    @Override
    public boolean saved(Reservation reservation) {
        reservation.setStatus(0);
        LocalDateTime lt = LocalDateTimeUtil.of(LocalDateTime.now());
        DateTime date = DateUtil.date(lt);
        reservation.setCreateTime(date);
        if(orm.saved(reservation)){
            return true;
        }
        return false;
    }

    @Override
    public TableDataInfo<Reservation> reservationList(OrdersBody ob) {
        Page<Reservation> page = new Page(ob.getPageNum(), ob.getPageSize());
        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<>();
        if (ob.getStatus() != null){
        wrapper.eq(Reservation::getStatus, ob.getStatus());
        }
        Page<Reservation> resultPage = orm.selectPage(page, wrapper);
        TableDataInfo<Reservation> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("请求成功");
        tableDataInfo.setRows(resultPage.getRecords());
        tableDataInfo.setTotal(resultPage.getTotal());
        return tableDataInfo;
    }

    @Override
    public boolean ubi(Long id) {
        if(orm.ubi(id)){
            return true;
        }
        return false;
    }
}
